package saucaca.fragment;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;
import saucaca.CustomView.PagerSlidingTabStrip;
import saucaca.Slidingmenu.SlidingMenu;
import saucaca.Util.TagName;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class VideoFragmentList extends Fragment {
	String TAG = VideoFragmentList.class.getSimpleName();

	MainActivity mActivity;
	String strCateCode="";
	String strAreaCode="";
	String strTypeHot = TagName.TYPE_HOT;
	private PagerSlidingTabStrip tabs;
	ViewPager viewPager;
	String title;
	SongOnlineAdapter adapter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		getActivity().getActionBar().hide();
		View view = inflater.inflate(R.layout.viewpager_tabstrip, container, false);
		findViewById(view);
		Bundle b = getArguments();
		String strCate =null;
		if(b!=null)
		 strCate = b.getString(MainActivity.ID);
		if (strCate != null) {
			String[] str = BaseEngine.stringtoArray(strCate, TagName.keyBundle);
			strAreaCode = str[0];
			strCateCode = str[1];
			String CateName = str[2];
			title="Video - "+CateName;
		}
		mActivity.myActionBar.setMenu(title, false, false);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu(title, false, false);
	}

	private void findViewById(View view) {
		tabs=(PagerSlidingTabStrip) view.findViewById(R.id.tabs);
		viewPager=(ViewPager) view.findViewById(R.id.viewPager);
		adapter = new SongOnlineAdapter(getChildFragmentManager());
		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
		viewPager.setPageMargin(pageMargin);
		viewPager.setAdapter(adapter);
		viewPager.setCurrentItem(0);
		tabs.setViewPager(viewPager);

		tabs.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int position) {
				switch (position) {
				case 0:
					mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
					break;
				default:
					mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
					break;
				}
			}

		});
	}


	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mActivity = (MainActivity) getActivity();
	}
	public class SongOnlineAdapter extends FragmentStatePagerAdapter {
		String[] listType = { mActivity.getResources().getString(R.string.string_noibat),
				mActivity.getResources().getString(R.string.string_moinhat),
				mActivity.getResources().getString(R.string.string_xemnhieu) };

		public SongOnlineAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return listType[position];
		}

		@Override
		public int getCount() {
			return listType.length;
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public Fragment getItem(int i) {
			strTypeHot = TagName.TYPE_HOT;
			switch (i) {
			case 1:
				strTypeHot = TagName.TYPE_NEW;
				break;
			case 2:
				strTypeHot = TagName.TYPE_VIEW;
				break;
			}
			return new VideoList(strCateCode, "", "", strAreaCode, "", "", strTypeHot);
		}
	}
}
